/**
 * 定义数采仪管理模块
 */
(function(window, angular, undefined){
	var dataCollectionInstrumetManagerModule = angular.module('water.apps.common.stationInfo.dataCollectionInstrumetManager', ['water.entity.dataCollectionInstrumet',
	                                                                                                                                     'water.entity.monitorStation']);
	
	dataCollectionInstrumetManagerModule.controller('commonDataCollectionInstrumetManagerController',function($rootScope,$scope,$dataCollectionInstrumetService,
			$monitorStationService){
		
		/**
		 * 新建对话框
		 */
		$scope.createDialog;
		/**
		 * 编辑对话框
		 */
		$scope.editDialog;
		/**
		 * 详情对话框
		 */
		$scope.detailDialog;
		/**
		 * 新增数据
		 */
		$scope.dataCollectionInstrumetCreate={};
		/**
		 * 编辑数据
		 */
		$scope.dataCollectionInstrumetEdit={};
		/**
		 * 详情数据
		 */
		$scope.dataCollectionInstrumetDetail={};
		/**
		 * 数采仪列表数据
		 */
		$scope.dataCollectionInstrumetList=null;
		/**
		 * 站点数据
		 */
		$scope.StationData=null;
		/**
		 *新建站点数据
		 */
		$scope.stationSelecter=null;
		/**
		 * 编辑站点数据
		 */
		$scope.stationSelecterEdit=null;
		
		/**
		 * 站点选择数据
		 */
		$scope.stationSelectList = null;
		
		/**
		 * 监听站点列表点击事件
		 */
		$scope.dataPramChangedBroadCast =$scope.$on('dataPramChanged', function(event, data) {
			$scope.StationData=data;
			$scope._getDataCollectionInstrumet();
			$scope._getStationSelectList();
	    });
		
		/**
		 * 新建
		 */
		$scope.onCreateClick=function(){
			if($scope.stationSelectList.length==0) {
				$.messager.alert("不存在站点信息，无法新建数采仪","不存在站点信息，无法新建数采仪",'info');
				return;
			}
			$scope.dataCollectionInstrumetCreate = {
					ID:$scope.stationSelectList[0].v+"数据采集仪",
					SITE_ID:$scope.stationSelectList[0].k
			};
			$scope.createDialog.open();
		};
		/**
		 * 新建提交
		 */
		$scope.onSubmitCreateClick = function(data,$event){
			console.log(arguments);
			if($event && $event.error){
				return;
			}
			$dataCollectionInstrumetService.createModel(data).then(function(){
				$scope._getDataCollectionInstrumet();
				$.messager.alert("保存成功","保存成功",'info');
				$scope.createDialog.close();
			});
		}
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(data){
			$scope.dataCollectionInstrumetEdit = angular.copy(data);
			$scope.editDialog.open();
		}
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data,$event){
			$dataCollectionInstrumetService.updateModel(data).then(function(){
				$scope._getDataCollectionInstrumet();
				$.messager.alert("保存成功","保存成功",'info');
				$scope.editDialog.close();
			});
		}
		
		/**
		 * 详情按钮点击事件
		 */
		$scope.onDetailClick = function(data){
			$scope.dataCollectionInstrumetDetail = data;
			$scope.detailDialog.open();
		}
		
		/**
		 * 删除
		 */
		$scope.onSubmitDeleteClick=function(data){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$dataCollectionInstrumetService.deleteModel(data.GUID).then(function(){
						$scope._getDataCollectionInstrumet();
						$.messager.alert("删除成功","删除成功",'info');
					});			
				}
			});
		}
		
		/**
		 * 查询数采仪列表
		 */
		$scope._getDataCollectionInstrumet = function(){
			$dataCollectionInstrumetService.getList({STATION_ID:$scope.StationData.STATION_ID}).then(function(d){
				$scope.dataCollectionInstrumetList = d.data
			});
		}
		
		/**
		 * 获取站点列表，编辑或者新增的时候需要选择
		 */
		$scope._getStationSelectList = function(){
			$monitorStationService.getDomainList({STATION_ID:$scope.StationData.STATION_ID}).then(function(d){
				$scope.stationSelectList = d;
			});
		};
	});
}(window, angular));